#!/usr/bin/env python
# -*- coding: utf-8 -*-
#MIT License
#By: cocobear.cn@gmail.com

import urllib2,urllib,cookielib,httplib
import sys,re
import gzip,StringIO

bookmarks = {}

def cang2html(filename,bookmarks):
    f = open(filename,"w")
    f.write("""<!DOCTYPE NETSCAPE-Bookmark-file-1>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
<!--This is an automatically generated file.
It will be read and overwritten.
Code write by cocobear with Python
Edit the file carefully!-->
<TITLE>Generated by cang2html</TITLE>
""")
    f.write("<H1>Bookmarks</H1>\n")
    f.write("<DL><p>\n")
    for tag in bookmarks.keys():
        f.write("<DT><H3>"+tag+"</H3>\n")
        f.write("<DL><P>\n")

        for item in bookmarks[tag]:
            [url,name,desc] = item

            f.write("\t<DT><A HREF=\""+url+"\">"+name+"</A>\n")
            if desc:
                f.write("\t<DD>"+desc+"</DD>\n")

        f.write("</DL><P>\n")

    f.close()

def process_data(data,bookmarks,user):

    tables = re.findall('(<table .*?</table>)',data,re.S)
    for t in tables:
        result = re.findall('href="((?:http|ftp|https|file)://.+?)" target.+?lnk\d+">(.+?)</font>.+?dc\d+">(.*?)</div>.+?showATags\("%s","(.*?)",".*?"\)'%user,t,re.S)
        if result:
            (name,url,desc,tags) = result[0]
            if tags == '':
                tags = 'cant2html'
            for tag in tags.split(','):
                if not bookmarks.has_key(tag):
                    bookmarks[tag] = []
                bookmarks[tag].append([name,url,desc])

                    

def get_data(user,opener,page):
    url = "http://cang.baidu.com/"+user+"/page/"+str(page)
    gziped_data = opener.open(url).read()
    gziped_stream = StringIO.StringIO(gziped_data)
    data = gzip.GzipFile(fileobj=gziped_stream).read()
    return data.decode('gbk').encode('utf-8')


def init():
    httplib.HTTPConnection.debuglevel  =  1 
    cookie = cookielib.CookieJar()
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))

    exheaders = [("User-Agent","Opera/9.27 (X11; Linux x86_64; U; en)"),("Connection","Keep-Alive"),("Referer","http://cang.baidu.com"),("Accept","text/html, application/xml;q=0.9, application/xhtml+xml, */*;q=0.1"),("Accept-Charset","iso-8859-1, utf-8, utf-16, *;q=0.1"),("Cookie2","$Version=1"),("Accept-Encoding","deflate, gzip, x-gzip, identity, *;q=0"),]

    opener.addheaders = exheaders
    urllib2.install_opener(opener)
    return opener

   

def main(argv=None):
    global bookmarks

    
    user = raw_input("Please input user name:")
    opener=init()
    data = get_data(user,opener,1)
    match = re.search("（共(\d+)条）",data)
    if match:
        total = int(match.group(1))
    else:
        print "User has no bookmars!"
        return 1
    print "Total number: %d " % total
    print "Starting ......"


    print "process page number: 1"
    process_data(data,bookmarks,user)
    if total % 10 != 0:
        for i in range(1,10):
            if (total+i) % 10 == 0:
                total+=i
    total/=10

    for i in range(2,total+1):
        data = get_data(user,opener,i)
        print "process page number: %d" % i
        process_data(data,bookmarks,user)


    for i in  bookmarks.keys():
        s = "%s :%d" % (i,len(bookmarks[i]))
        print(s.decode('utf-8'))
    
    cang2html(user+".html",bookmarks)
    print "Succeed!\nCheck %s.html for your bookmarks" % user


if __name__ == "__main__":
    sys.exit(main())
